﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using RH.Common.Extensions;
using RH.Domain;
using RH.Web.ViewModels;
using RH.Web.Utility;
using RH.Web.ViewModels.UtilityModels;
using AutoMapper;

namespace RH.Web.Areas.Admin.Controllers
{
	[Authorize(Roles="Admin")]
    public class LeadController : BaseController
    {
		private AppEntities _Context = null;
		private EntityRepository _Repository = null;
        
		#region Constructors
		public LeadController()
		{
			_Context = new AppEntities();
			_Repository = new EntityRepository(_Context);
		}

		public LeadController(UserViewModel currentUser)
			: base(currentUser)
        {
			_Context = new AppEntities();
			_Repository = new EntityRepository(_Context);
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View("List", BuildLeadListConfig());
        }

		public ActionResult List()
        {
            return View(BuildLeadListConfig());
        }

		/// <summary>
        /// Gets a Lead from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0, String IsSaved = null)
        {
            Lead entity = id == 0 ? new Lead() : _Context.Leads.FirstOrDefault(P => P.ID == id);            
            LeadViewModel viewModel = Mapper.Map<Lead, LeadViewModel>(entity);             
            if (String.Compare(IsSaved, "true", true) == 0)
                ViewData[AppConstants.ViewDataKeys.SAVED] = true;
			return View(viewModel);
        }

		/// <summary>
        /// Saves Lead Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(LeadViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Lead entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new Lead();
                _Context.AddToLeads(entity);
            }
            else
                entity = _Context.Leads.FirstOrDefault(P => P.ID == viewModel.ID);

            entity = Mapper.Map<LeadViewModel, Lead>(viewModel, entity);
		
            _Context.SaveChanges();
            //return RedirectToAction("Index");
			return RedirectToAction(String.Format("Edit/{0}", entity.ID), new { IsSaved = "true" });
        }		
		#endregion Action Methods
		
		#region Helper Methods

		/// <summary>
        /// Builds a ListConfig object with a list of Lead
        /// </summary>
        /// <returns></returns>
		private ListConfig BuildLeadListConfig()
		{
			UtilityDAO dao = new UtilityDAO();
            //RH.Domain.DbParameter[] parameters = new[]{
                //new RH.Domain.DbParameter("@SiteUserID", LoggedInUser.ID)
            //};
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_LEAD_LIST_BY_SERVICE_PERIOD, null, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], null);
            return listConfig;
		}

        #endregion			
    }
}


